﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using RA.UI.Messages;
using Microsoft.Win32;
using RA.UI.Model;
using RA.Services;
using System.Collections.ObjectModel;
using RA.Common;

namespace RA.UI.ViewModel
{
    public class TemplatesManagerViewModel : ViewModelBase
    {
        #region private
        string _defaultPath;
        TemplatesManagerModel _model;
        #endregion private

        public TemplatesManagerViewModel(TemplatesManagerModel model)
        {
            _model = model;
            _defaultPath = AppDomain.CurrentDomain.BaseDirectory + "Templates";

            Messenger.Default.Register<ShowImageTemplatesMessage>(this,
                (mess) => FoundTemplates = GetFoundTemplates(mess.Templates));
        }

        #region Props
        ObservableCollection<TemplatesItemViewModel> _templates;
        public ObservableCollection<TemplatesItemViewModel> Templates
        {
            get { return _templates; }

            set
            {
                if (_templates != value)
                {
                    _templates = value;
                    RaisePropertyChanged(() => Templates);
                }
            }
        }

        ObservableCollection<TemplatesItemViewModel> _foundTemplates;
        public ObservableCollection<TemplatesItemViewModel> FoundTemplates
        {
            get { return _foundTemplates; }

            set
            {
                if (_foundTemplates != value)
                {
                    _foundTemplates = value;
                    RaisePropertyChanged(() => FoundTemplates);
                }
            }
        }

        string _selectedPath;
        public string SelectedPath
        {
            get { return _selectedPath; }

            set
            {
                if (_selectedPath != value)
                {
                    _selectedPath = value;

                    InitTemplates(value);

                    RaisePropertyChanged(() => SelectedPath);
                }
            }
        }
        #endregion Props

        #region Commands
        ICommand _openNewTemplateFile;
        public ICommand OpenNewTemplateFile
        {
            get 
            { 
                if(_openNewTemplateFile == null)
                    _openNewTemplateFile = new RelayCommand(() => OpenFileDialog());
                return _openNewTemplateFile; 
            }
        }

        ICommand _findAllTemplatesCommand;
        public ICommand FindAllTemplatesCommand
        {
            get 
            {
                if (_findAllTemplatesCommand == null)
                    _findAllTemplatesCommand = new RelayCommand(() => 
                        Messenger.Default.Send(new ShowTemplateAllRectanglesMessage()),
                        () => FoundTemplates != null ? FoundTemplates.Count > 0 : false);
                return _findAllTemplatesCommand; 
            }
        }

        //ICommand _findTemplateCommand;
        //public ICommand FindTemplateCommand
        //{
        //    get
        //    {
        //        if (_findTemplateCommand == null)
        //            _findTemplateCommand = new RelayCommand<object>((param) =>
        //                {
        //                    if (param is TemplatesItemViewModel)
        //                    {
        //                        var tivm = (TemplatesItemViewModel)param;
        //                        Messenger.Default.Send(new ShowTemplateRectangleMessage(tivm.Template)); //TODO
        //                    }
        //                }
        //            );


        //        return _findTemplateCommand;
        //    }
        //}
        #endregion Commands

        #region Utils
        void OpenFileDialog()
        {
            var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
            dialog.Filter = "Файлы шаблонов (*.bin)|*.bin";
            if (dialog.ShowDialog() == true)
            {
                SelectedPath = dialog.FileName;
            }
        }

        ObservableCollection<TemplatesItemViewModel> GetFoundTemplates(Templates templates)
        {
            Action<TemplatesItemViewModel> removeAction = new Action<TemplatesItemViewModel>((t) =>
            {
                Templates.Remove(t);
                var saveTemplates = new Templates();
                saveTemplates.AddRange(Templates.Select(tt => tt.Template));
                _model.SaveTemplates(saveTemplates, SelectedPath);
            }
            );

            Action<TemplatesItemViewModel> addAction = new Action<TemplatesItemViewModel>((t) =>
            {
                FoundTemplates.Remove(t);
                t.AddToSelectedAction = null;
                t.RemoveFromSelectedAction = removeAction;
                Templates.Add(t);
                var saveTemplates = new Templates();
                saveTemplates.AddRange(Templates.Select(tt => tt.Template));
                _model.SaveTemplates(saveTemplates, SelectedPath);
            }
            );

            return new ObservableCollection<TemplatesItemViewModel>(
                        templates.Select(t => new TemplatesItemViewModel(t)
                        {
                            AddToSelectedAction = addAction
                        }
                        ));
        }

        void InitTemplates(string fileName)
        { 
            Action<TemplatesItemViewModel> removeAction = new Action<TemplatesItemViewModel>((t) => 
                {
                    Templates.Remove(t);
                    var saveTemplates = new Templates();
                    saveTemplates.AddRange(Templates.Select(tt => tt.Template));
                    _model.SaveTemplates(saveTemplates, fileName);
                }
            );
            Templates = new ObservableCollection<TemplatesItemViewModel>(
                        _model.GetTemplates(fileName).Select(t => new TemplatesItemViewModel(t)
                        {
                            RemoveFromSelectedAction = removeAction
                        }
                        ));
        }
        #endregion Utils
    }
}
